import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class GameWin extends JFrame {

    /** 游戏状态 0未开始,1游戏中,2通关失败,3通关成功,4暂停,5重新开始*/

    //定义游戏默认状态
    static int state = 0;

    Image offScreenImage;

    //宽高
    static int width = 1440;
    static int height = 900;
    //背景
    Background bg= new Background();

    double random;
    //计数器
    int time = 0;

    //敌方鱼
    Enemy enemy;
    //boss类
    Enemy boss;
    //是否生成boss
    boolean isBoss = false;

    //我方鱼。
    MyFish myFish = new MyFish();


    // 音频播放器
    private Clip backgroundMusic;


    public void launch(){
        this.setVisible(true);
        this.setSize(width,height);
        this.setLocationRelativeTo(null);
        this.setTitle("大鱼吃小鱼");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 加载并播放背景音乐
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src\\backgroundmusic.wav"));
            backgroundMusic = AudioSystem.getClip();
            backgroundMusic.open(audioInputStream);
            backgroundMusic.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (e.getButton()==1&&state==0){
                    state=1;
                    repaint();
                }
                if(e.getButton()==1&&(state==2||state==3)){
                    reGame();
                    state=1;
                }
            }
        });
    //键盘移动
        this.addKeyListener(new KeyAdapter() {
            @Override
            //按压
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == 87) {
                    GameUtils.UP = true;
                }
                if (e.getKeyCode() == 83) {
                    GameUtils.DOWN = true;
                }
                if (e.getKeyCode() == 65) {
                    GameUtils.LEFT = true;
                }
                if (e.getKeyCode() == 68) {
                    GameUtils.RIGHT = true;
                }
                if(e.getKeyCode()==32){
                    switch (state){
                        case 1:
                            state = 4;
                            GameUtils.drawWord(getGraphics(),"游戏暂停!!!",Color.red,50,600,400);
                            break;
                        case 4:
                            state = 1;
                            break;
                    }
                }
            }
            //释放
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (e.getKeyCode() == 87) {
                    GameUtils.UP = false;

                }
                if (e.getKeyCode() == 83) {
                    GameUtils.DOWN = false;
                }
                if (e.getKeyCode() == 65) {
                    GameUtils.LEFT = false;
                }
                if (e.getKeyCode() == 68) {
                    GameUtils.RIGHT = false;
                }

            }
        });

        while (true){
            repaint();
            time++;
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void paint(Graphics g) {
        //懒加载模式初始化对象
        offScreenImage = createImage(width,height);
        Graphics gImage = offScreenImage.getGraphics();
        bg.paintSelf(gImage,myFish.level);

        switch (state){
            case 0:

                break;
            case 1:
                myFish.paintSelf(gImage);
                logic();
                for(Enemy enemy:GameUtils.EnemyList){
                    enemy.paintSelf(gImage);
                }
                if(isBoss){
                    boss.x=boss.x+boss.direction*boss.speed;
                    boss.paintSelf(gImage);
                    if(boss.x<0){
                        gImage.setColor(Color.RED);
                        gImage.fillRect(boss.x,boss.y,2400,boss.height/30);
                    }
                }
                break;
            case 2:
                for(Enemy enemy:GameUtils.EnemyList){
                    enemy.paintSelf(gImage);
                }
                if(isBoss){
                    boss.paintSelf(gImage);
                }
                break;
            case 3:
                myFish.paintSelf(gImage);
                break;
            case 4:
                return;
                default:
        }

        g.drawImage(offScreenImage,0,0,null);

    }

    void logic() {
        //关卡难度
        if(GameUtils.count<5){
            GameUtils.level=0;
            myFish.level=1;
        } else if (GameUtils.count<=15) {
            GameUtils.level=1;
        } else if (GameUtils.count<=50) {
            GameUtils.level=2;
            myFish.level=2;
        } else if (GameUtils.count<=150) {
            enemy = new Enemy_3_L();
            enemy.width = 150;
            enemy.height = 75;
            enemy.speed = 50;
            enemy.count = 1;
            enemy.type = 1;
            GameUtils.EnemyList.add(enemy);
            GameUtils.level=3;
            myFish.level=3;
        } else if (GameUtils.count<=300) {
            GameUtils.level=4;
            myFish.level=3;
        } else if (GameUtils.count>300) {
            state = 3;
        }
        //定义随机数，让左方敌方鱼和右方敌方鱼有一半几率从左右两方出来
        random = Math.random();
        switch(GameUtils.level){
            case 4:
                if (time % 60 == 0) {
                    if(random>0){
                        boss=new Enemy_boss();
                        isBoss=true;
                    }
                }
            case 3:
            case 2:
                if (time % 30 == 0) {
                    if (random > 0.5) {
                        enemy = new Enemy_3_L();
                    } else {
                        enemy = new Enemy_3_R();
                    }
                    GameUtils.EnemyList.add(enemy);
                }
            case 1:
                if (time % 20 == 0) {
                    if (random > 0.5) {
                        enemy = new Enemy_2_L();
                    } else {
                        enemy = new Enemy_2_R();
                    }
                    GameUtils.EnemyList.add(enemy);
                }
            case 0:
                //敌方鱼生成
                if (time % 10 == 0) {
                    if (random > 0.5) {
                        enemy = new Enemy_1_L();
                    } else {
                        //添加敌方鱼右
                        enemy = new Enemy_1_R();
                    }
                    GameUtils.EnemyList.add(enemy);
                }
                break;
            default:
        }
        //敌方鱼移动方向
        for (Enemy enemy : GameUtils.EnemyList) {
            enemy.x = enemy.x+enemy.direction*enemy.speed;
            if(isBoss){
                if(boss.getRec().intersects(enemy.getRec())){
                    enemy.x=-200;
                    enemy.y=-200;
                }
                if(boss.getRec().intersects(myFish.getRec())){
                    state=2;
                }
            }
            ///我方鱼与敌方鱼碰撞检测
            if(myFish.getRec().intersects(enemy.getRec())){
                //添加碰撞条件
                if(myFish.level >= enemy.type) {
                    enemy.x=-200;
                    enemy.y=-200;
                    GameUtils.count=GameUtils.count+ enemy.count;
                } else {
                    state = 2;
                }
            }
        }
    }
    //重新开始
    void reGame(){
        GameUtils.EnemyList.clear();
        time=0;
        myFish.level=1;
        GameUtils.count=0;
        myFish.x=700;
        myFish.y=500;
        myFish.width=50;
        myFish.height=50;
        boss=null;
        isBoss = false;
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
